package cn.songtx.iot.controller.Sensor;

import cn.songtx.iot.pojo.SensorAem1000;
import cn.songtx.iot.pojo.Result;
import cn.songtx.iot.service.ISensorAem1000Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/AEM1000")
@RequiredArgsConstructor
public class Aem1000Controller {

    private final ISensorAem1000Service sensorAem1000Service;

    /**
     * 用get方法,Params传入mac,查询最后一组数据
     * 使用条件构造器,构造根据id倒序排列,匹配mac,最后一条
     *
     * @param mac 设备的mac地址
     * @return 统一的Result
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public Result aem1000GetOne(String mac) {
        LambdaQueryWrapper<SensorAem1000> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SensorAem1000::getId)
                .eq(SensorAem1000::getMac, mac)
                .last("limit 1");
        SensorAem1000 one = sensorAem1000Service.getOne(queryWrapper);
        return Result.success(one);
    }

    /**
     * 带mac条件的分页查询
     * 使用分页插件完成分页功能 mybatisplus.extension.plugins.pagination.Page;
     * POST请求体中携带分页对象{"current":页码,"size":每页几条数据},用map接收
     *
     * @param mac     设备的mac地址
     * @param current 页码
     * @param size    第几页
     * @return 统一的Result
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    public Result equipmentPages(String mac, Long current, Long size) {
        // 1.创建分页对象,并传入分页参数
        IPage<SensorAem1000> iPage = new Page<>(current, size);
        // 2.查询条件,mac
        LambdaQueryWrapper<SensorAem1000> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SensorAem1000::getId).eq(SensorAem1000::getMac, mac);
        // 3.调用分页方法,返回分页对象
        IPage<SensorAem1000> page = sensorAem1000Service.page(iPage, lambdaQueryWrapper);
        return Result.success(page);
    }
}
